var t = require;
var e = module;
var n = exports;
Object.defineProperty(n, "__esModule", {value: !0}), (n.Files = void 0);
var o = (function () {
    function t() {}
    return (
        (t.init = function (t) {
            if (((this.标记为删除 = {}), "undefined" != typeof oppoqg)) (this.files = new a()), t();
            else if ("undefined" != typeof vivoqg) (this.files = new i()), t();
            else if ("undefined" != typeof huaweiqg) (this.files = new s()), t();
            else {
                if ("undefined" == typeof wx) return void t();
                this.files = new r(t);
            }
            this.ress = window.ress;
        }),
        (t.downloadFileByUUID = function (t, e) {
            this.files ? this.files.downloadFile(this._ress[t], e) : e && e();
        }),
        (t.loadCDN = function (t, e) {
            this.loadCDNRess(!0, t, e);
        }),
        (t.loadCDNRess = function (t, e, n) {
            var o = this,
                r = window.cdnResName;
            this.files.downloadFile(r, function () {
                var a = o.files.readStr(o.cdnPath + r);
                try {
                    o._ress = JSON.parse(a);
                } catch (t) {
                    o._ress = null;
                }
                if (o._ress) {
                    for (var i in o._ress) o.ress[i] = o.cdnPath + o._ress[i];
                    if (e) {
                        var s = 0;
                        for (var i in o._ress) s++;
                        if (s > 0)
                            for (var i in o._ress)
                                o.downloadFileByUUID(i, function () {
                                    --s <= 0 && n && n();
                                });
                        else n && n();
                    } else n && n();
                } else t && (console.error("加载列表页出错则删除本地文件并重试 " + r), (o.标记为删除[r] = !0), o.loadCDNRess(!1, e, n));
            });
        }),
        (t.loadImg = function (e, n, o) {
            if (n && this.files) {
                var r = e.replace(/^.+\/([^\/]+)$/, "$1");
                this.files.loadImg(e, t.cdnPath + r, o);
            } else this._loadImg(e, o);
        }),
        (t._loadImg = function (t, e) {
            this.cacheEnabled || cc.assetManager.cacheManager.removeCache(t);
            var n = function (t) {
                    clearTimeout(o), e && e(t), (e = null);
                },
                o = setTimeout(function () {
                    console.error("加载超时：" + t), n(null);
                }, 1e4);
            try {
                cc.assetManager.loadRemote(t, function (e, o) {
                    e ? (console.error("加载失败：" + t), n(null)) : n(o);
                });
            } catch (e) {
                console.error("加载出错：" + t), n(null);
            }
        }),
        (t.cacheEnabled = !0),
        t
    );
})();
n.Files = o;
var r = (function () {
        function t(t) {
            var e = this;
            if (
                ((this.fs = wx.getFileSystemManager()),
                (this.userResPath = wx.env.USER_DATA_PATH + "/res/"),
                (o.cdnPath = this.userResPath + "cdn/"),
                window.compressImports)
            ) {
                this.importsPath = this.userResPath + "imports/";
                new Date().getTime();
                this.makeDir(wx.env.USER_DATA_PATH, function () {
                    e.makeDir(e.userResPath, function () {
                        e.makeDir(o.cdnPath, function () {
                            e.makeDir(e.importsPath, function () {
                                e.unzipImports(t);
                            });
                        });
                    });
                });
            } else t();
        }
        return (
            (t.prototype.readStr = function (t) {
                return this.fs.readFileSync(t, "utf-8");
            }),
            (t.prototype.loadImg = function (t, e, n) {
                this.fs.access({
                    path: e,
                    success: function () {
                        o._loadImg(e, n);
                    },
                    fail: function () {
                        wx.downloadFile({
                            url: t,
                            filePath: e,
                            success: function (t) {
                                o._loadImg(e, n);
                            },
                            fail: function (n) {
                                console.error("下载失败 " + t + "\n" + e + "\n" + JSON.stringify(n));
                            }
                        });
                    }
                });
            }),
            (t.prototype.downloadFile = function (t, e) {
                var n = this;
                o.标记为删除[t]
                    ? (delete o.标记为删除[t], this._downloadFile(t, e))
                    : this.fs.access({
                          path: o.cdnPath + t,
                          success: e,
                          fail: function () {
                              n._downloadFile(t, e);
                          }
                      });
            }),
            (t.prototype._downloadFile = function (t, e) {
                var n = window.cdnResRoot + t;
                console.log("downloadFile " + n),
                    wx.downloadFile({
                        url: n,
                        filePath: o.cdnPath + t,
                        success: function (t) {
                            e();
                        },
                        fail: function (e) {
                            console.error("下载失败 " + n + "\n" + o.cdnPath + t + "\n" + JSON.stringify(e));
                        }
                    });
            }),
            (t.prototype.unzipImports = function (t) {
                var e,
                    n = this;
                new Date().getTime();
                "百度小游戏" == window.platformType
                    ? ((e = this.userResPath + "imports.zip"),
                      this.fs.writeFileSync(e, this.fs.readFileSync("/assets/1st/imports.zip")))
                    : (e = "/assets/1st/imports.zip"),
                    this.fs.unzip({
                        zipFilePath: e,
                        targetPath: this.importsPath,
                        success: function (e) {
                            (window.solveZipURL = function (t) {
                                return t.replace(window.solveZipBundleNamesReg, n.importsPath);
                            }),
                                t();
                        },
                        fail: function (t) {
                            console.error("解压失败", t);
                        }
                    });
            }),
            (t.prototype.makeDir = function (t, e) {
                var n = this;
                this.fs.access({
                    path: t,
                    success: function () {
                        e();
                    },
                    fail: function () {
                        n.fs.mkdir({
                            dirPath: t,
                            success: function (t) {
                                console.log("创建目录成功", t), e();
                            },
                            fail: function (t) {
                                console.error("创建目录失败", t);
                            }
                        });
                    }
                });
            }),
            t
        );
    })(),
    a = (function () {
        function t() {
            (this.fs = oppoqg.getFileSystemManager()),
                (o.cdnPath = oppoqg.env.USER_DATA_PATH + "/res/cdn/"),
                this.fs.mkdir({
                    dirPath: o.cdnPath,
                    success: function (t) {},
                    fail: function (t) {
                        console.error("创建目录失败" + JSON.stringify(t));
                    }
                });
        }
        return (
            (t.prototype.readStr = function (t) {
                return this.fs.readFileSync(t, "utf-8");
            }),
            (t.prototype.loadImg = function (t, e, n) {
                this.fs.access({
                    path: e,
                    success: function () {
                        o._loadImg(e, n);
                    },
                    fail: function () {
                        oppoqg.downloadFile({
                            url: t,
                            filePath: e,
                            success: function (t) {
                                o._loadImg(e, n);
                            },
                            fail: function (n) {
                                console.error("下载失败 " + t + "\n" + e + "\n" + JSON.stringify(n));
                            }
                        });
                    }
                });
            }),
            (t.prototype.downloadFile = function (t, e) {
                var n = this;
                o.标记为删除[t]
                    ? (delete o.标记为删除[t], this._downloadFile(t, e))
                    : this.fs.access({
                          path: o.cdnPath + t,
                          success: e,
                          fail: function () {
                              n._downloadFile(t, e);
                          }
                      });
            }),
            (t.prototype._downloadFile = function (t, e) {
                var n = window.cdnResRoot + t;
                oppoqg.downloadFile({
                    url: n,
                    filePath: o.cdnPath + t,
                    success: function (t) {
                        e();
                    },
                    fail: function (e) {
                        console.error("下载失败 " + n + "\n" + o.cdnPath + t + "\n" + JSON.stringify(e));
                    }
                });
            }),
            t
        );
    })(),
    i = (function () {
        function t() {
            (o.cdnPath = "internal://files/cdn/"),
                vivoqg.mkdir({
                    uri: o.cdnPath,
                    success: function (t) {},
                    fail: function (t, e) {
                        console.error("创建目录失败", t, e);
                    }
                });
        }
        return (
            (t.prototype.readStr = function (t) {
                return vivoqg.readFileSync({uri: t, encoding: "utf8"}).text;
            }),
            (t.prototype.loadImg = function (t, e, n) {
                "true" == vivoqg.accessFile({uri: e})
                    ? o._loadImg(e, n)
                    : vivoqg.download({
                          url: t,
                          success: function (r) {
                              vivoqg.copyFile({
                                  srcUri: r.tempFilePath,
                                  dstUri: e,
                                  success: function (t) {
                                      o._loadImg(e, n);
                                  },
                                  fail: function (n, o) {
                                      console.error("复制失败 " + t + "\n" + e, n, o);
                                  }
                              });
                          },
                          fail: function (n, o) {
                              console.error("下载失败 " + t + "\n" + e, n, o);
                          }
                      });
            }),
            (t.prototype.downloadFile = function (t, e) {
                o.标记为删除[t]
                    ? (delete o.标记为删除[t], this._downloadFile(t, e))
                    : "true" == vivoqg.accessFile({uri: o.cdnPath + t})
                    ? e()
                    : this._downloadFile(t, e);
            }),
            (t.prototype._downloadFile = function (t, e) {
                var n = window.cdnResRoot + t;
                vivoqg.download({
                    url: n,
                    success: function (r) {
                        vivoqg.copyFile({
                            srcUri: r.tempFilePath,
                            dstUri: o.cdnPath + t,
                            success: function (t) {
                                e();
                            },
                            fail: function (e, r) {
                                console.error("复制失败 " + n + "\n" + o.cdnPath + t, e, r);
                            }
                        });
                    },
                    fail: function (e, r) {
                        console.error("下载失败 " + n + "\n" + o.cdnPath + t, e, r);
                    }
                });
            }),
            t
        );
    })(),
    s = (function () {
        function t() {
            (this.fs = huaweiqg.getFileSystemManager()),
                (o.cdnPath = huaweiqg.env.USER_DATA_PATH + "/res/cdn/"),
                this.fs.mkdir({
                    dirPath: o.cdnPath,
                    recursive: !0,
                    success: function (t) {},
                    fail: function (t) {
                        console.error("创建目录失败" + JSON.stringify(t));
                    }
                });
        }
        return (
            (t.prototype.readStr = function (t) {
                return this.fs.readFileSync(t, "utf-8");
            }),
            (t.prototype.loadImg = function (t, e, n) {
                this.fs.access({
                    path: e,
                    success: function () {
                        o._loadImg(e, n);
                    },
                    fail: function () {
                        huaweiqg.downloadFile({
                            url: t,
                            filePath: e,
                            success: function (t) {
                                o._loadImg(e, n);
                            },
                            fail: function (n) {
                                console.error("下载失败 " + t + "\n" + e + "\n" + JSON.stringify(n));
                            }
                        });
                    }
                });
            }),
            (t.prototype.downloadFile = function (t, e) {
                var n = this;
                o.标记为删除[t]
                    ? (delete o.标记为删除[t], this._downloadFile(t, e))
                    : this.fs.access({
                          path: o.cdnPath + t,
                          success: e,
                          fail: function () {
                              n._downloadFile(t, e);
                          }
                      });
            }),
            (t.prototype._downloadFile = function (t, e) {
                var n = window.cdnResRoot + t;
                huaweiqg.downloadFile({
                    url: n,
                    filePath: o.cdnPath + t,
                    success: function (t) {
                        e();
                    },
                    fail: function (e) {
                        console.error("下载失败 " + n + "\n" + o.cdnPath + t + "\n" + JSON.stringify(e));
                    }
                });
            }),
            t
        );
    })();
window.Files = o;
